package com.ruoyi.citylife.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.citylife.domain.bo.CUserBo;
import com.ruoyi.citylife.domain.vo.CUserVo;
import com.ruoyi.citylife.domain.CUser;
import com.ruoyi.citylife.mapper.CUserMapper;
import com.ruoyi.citylife.service.ICUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信用户Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-31
 */
@Service
public class CUserServiceImpl extends ServicePlusImpl<CUserMapper, CUser, CUserVo> implements ICUserService {

    @Override
    public CUserVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<CUserVo> queryPageList(CUserBo bo) {
        PagePlus<CUser, CUserVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<CUserVo> queryList(CUserBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<CUser> buildQueryWrapper(CUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), CUser::getOpenid, bo.getOpenid());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), CUser::getNickname, bo.getNickname());
        lqw.eq(bo.getSex() != null, CUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), CUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), CUser::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getSign()), CUser::getSign, bo.getSign());
        lqw.eq(bo.getDisabled() != null, CUser::getDisabled, bo.getDisabled());
        lqw.eq(bo.getLastLoginTime() != null, CUser::getLastLoginTime, bo.getLastLoginTime());
        lqw.eq(bo.getCheats() != null, CUser::getCheats, bo.getCheats());
        lqw.eq(bo.getDeleted() != null, CUser::getDeleted, bo.getDeleted());
        return lqw;
    }

    @Override
    public Boolean insertByBo(CUserBo bo) {
        CUser add = BeanUtil.toBean(bo, CUser.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(CUserBo bo) {
        CUser update = BeanUtil.toBean(bo, CUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
